package com.mall.db.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsHomeBrandMapper;
import com.mall.pojo.SmsHomeBrand;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class SmsHomeBrandApiImpl implements SmsHomeBrandApi {

    @Autowired
    private SmsHomeBrandMapper homeBrandMapper;

    /**
     * 查询推荐品牌分页信息
     *
     * @param pageNum
     * @param pageSize
     * @param brandName
     * @param recommendStatus
     * @return
     */
    @Override
    public Page<SmsHomeBrand> list(Integer pageNum, Integer pageSize, String brandName, Integer recommendStatus) {
        Page<SmsHomeBrand> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(brandName), SmsHomeBrand::getBrandName, brandName)
                .eq(ObjectUtil.isNotEmpty(recommendStatus), SmsHomeBrand::getRecommendStatus, recommendStatus)
                .orderByDesc(SmsHomeBrand::getSort);
        return homeBrandMapper.selectPage(page,wrapper);
    }

    /**
     * 根据一组id查询品牌信息
     * @param ids
     * @return
     */
    @Override
    public List<SmsHomeBrand> findBrandByIds(Long[] ids) {
        LambdaQueryWrapper<SmsHomeBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeBrand::getId,ids);
        return homeBrandMapper.selectList(wrapper);
    }

    /**
     * 修改品牌信息
     * @param brand
     */
    @Override
    public Integer updateBrand(SmsHomeBrand brand) {
        int count = homeBrandMapper.updateById(brand);
        return count;
    }

    /**
     * 根据品牌id查询推荐品牌信息
     * @param brandId
     * @return
     */
    @Override
    public SmsHomeBrand findBrandByBrandId(Long brandId) {
        LambdaQueryWrapper<SmsHomeBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsHomeBrand::getBrandId,brandId);
        return homeBrandMapper.selectOne(wrapper);
    }

    /**
     * 添加推荐品牌
     * @param brand
     */
    @Override
    public void create(SmsHomeBrand brand) {
        homeBrandMapper.insert(brand);
    }

    /**
     * 根据id查询推荐品牌信息
     * @param id
     * @return
     */
    @Override
    public SmsHomeBrand findBrandById(Integer id) {
        return homeBrandMapper.selectById(id);
    }

    /**
     * 根据id删除推荐品牌信息
     * @param ids
     * @return
     */
    @Override
    public Integer deleteByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        return homeBrandMapper.deleteBatchIds(idList);
    }
}
